/*
 * Copyright (c) 2019 DawnFramework. All Rights Reserved.
 * Licensed under the MIT (LICENSE) license.
 */

package org.dawn.common.service.service;

import org.dawn.common.core.except.IError;
import org.dawn.common.core.mode.ToString;
import org.dawn.common.service.except.ApiException;

/**
 * @author Willard Hu on 2017/4/25.
 */
public class ServiceResponse extends ToString {
    private static final long serialVersionUID = 6015541423080025328L;

    public static final Integer SUCCESS_CODE = 200;

    protected Integer statusCode;
    protected IError<Integer> error;

    protected void initSuccessData() {
        setStatusCode(SUCCESS_CODE);
    }

    protected void initFailedData(IError<Integer> err) {
        setStatusCode(err.errcode());
        setError(err);
    }

    public boolean isSuccess() {
        return SUCCESS_CODE.equals(statusCode);
    }

    public void check() {
        if (!isSuccess()) {
            throw new ApiException(error);
        }
    }

    public Integer getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }

    public IError getError() {
        return error;
    }

    public void setError(IError error) {
        this.error = error;
    }
}
